package com.zara.store.web.struts;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zara.store.client.clientmodel.Basket;

public class BasketAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		HttpSession session = request.getSession();
		Basket basket = (Basket) session.getAttribute(Basket.class.getName());
		
		if(basket != null && form != null) {
			BasketForm customForm = (BasketForm) form;
			BasketActionHelper helper = new BasketActionHelper(customForm);
			String[] productAmount = customForm.getProductAmount();
			
			if(productAmount != null) {
				for(int i = 0; i < productAmount.length; i++) {
					if(!helper.isProductRemoved(i)) {
						boolean productOffered = helper.isProductOffered(i);
						basket.changeProductOfferAndAmount(i, productOffered, productAmount[i]);
						try {
							int a = Integer.parseInt(productAmount[i]);
							if(0 == a) {
								helper.addRemoved(i);
							}
						} catch(NumberFormatException e) {
						}
					}
				}
			}
			if(helper.hasRemoved()) {
				for(int i = productAmount.length-1; i >= 0; i--) {
					if(helper.isProductRemoved(i)) {
						basket.removeProduct(i);
					}
				}
			}
			if("Facturar >".equals(customForm.getSubmit())) {
				return (mapping.findForward("invoice"));
			}
		}
		
		return (mapping.findForward("success"));
	}


	public static class BasketActionHelper {

		Map<String, String> removed;
		Map<String, String> offered;

		public BasketActionHelper(BasketForm customForm) {
			removed = new HashMap<String, String>();
			offered = new HashMap<String, String>();
			String[] productOffer = customForm.getProductOffer();
			String[] productRemove = customForm.getProductRemove();
			if(productOffer != null) {
				for(String offer: productOffer) {
					offered.put(offer, offer);
				}
			}
			if(productRemove != null) {
				for(String remove: productRemove) {
					addRemoved(remove);
				}
			}
		}

		public boolean hasRemoved() {
			return !removed.isEmpty();
		}

		public Map<String, String> getRemoved() {
			return removed;
		}

		public boolean isProductRemoved(int i) {
			return removed.containsKey(""+i);
		}

		public boolean isProductOffered(int i) {
			return offered.containsKey(""+i);
		}
		
		public void addRemoved(String remove) {
			removed.put(remove, remove);
		}
		
		public void addRemoved(int remove) {
			removed.put(""+remove, ""+remove);
		}
		
	}

}